package com.cube.truthseer.service.impl;

import com.cube.truthseer.common.entity.enums.ResultCode;
import com.cube.truthseer.common.entity.vo.ResultMessage;
import com.cube.truthseer.common.exception.BadRequestException;
import com.cube.truthseer.common.minio.MinIOUtil;
import com.cube.truthseer.common.security.AuthUser;
import com.cube.truthseer.common.security.context.UserContext;
import com.cube.truthseer.common.security.token.TokenUtil;
import com.cube.truthseer.common.utils.ResultUtil;
import com.cube.truthseer.common.utils.StringUtil;
import com.cube.truthseer.common.utils.UserRequest;
import com.cube.truthseer.entity.video.enums.VideoDeleteStatus;
import com.cube.truthseer.entity.video.enums.VideoFlagStatus;
import com.cube.truthseer.entity.video.po.Video;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cube.truthseer.mapper.VideoMapper;
import com.cube.truthseer.service.IVideoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.mapping.ResultMap;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;

import static com.cube.truthseer.common.entity.constants.ErrorInfo.Msg.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zw
 * @since 2024-03-09
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements IVideoService {
    private final MinIOUtil minIOUtil;
    private final TokenUtil tokenUtil;
    @Transactional
    @Override
    public ResultMessage<Object> insertVideo(MultipartFile multipartFile) {

        log.info("multipartFile:{}",multipartFile);
        String token = UserRequest.getCurrentToken();
        log.info("token:"+token);
        AuthUser authUser = tokenUtil.parseToken(token);
        String userId =String.valueOf( authUser.getId());
        //1.判断用户是否登录
        if(userId==null|| StringUtil.isBlank(userId)){
            throw new BadRequestException(OPERATE_FAILED);
        }
        //2.判断是否上传了图片
        if(multipartFile==null){
            throw  new BadRequestException(REQUEST_PARAM_ILLEGAL);
        }
//        String  locate = minIOUtil.uploadImgFile(userId, prefix, multipartFile, null);
        String locate = minIOUtil.upload(userId, userId, multipartFile);
        log.info("locate:{}",locate);
        Video video = new Video();
        video.setDelete(VideoDeleteStatus.NORMAL);
        video.setFlag(VideoFlagStatus.NORMAL);
        video.setUserId(Long.valueOf(userId));
        save(video);
        return ResultUtil.data(locate);


    }
}
